package com.gfxy.modules.statistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.statistics.service.CoreTotalScoreExcel;
import com.gfxy.modules.statistics.vo.CoreSummaryForSchoolVo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


@Controller("coreTotalScoreForOrgExcelView")
public class CoreTotalScoreForOrgExcelView extends MiniAbstractExcelView {
    public static final String FILE_NAME = "fileName";
    public static final String TITLE = "title";
    public static final String CONTENT = "content";


    @Autowired
    private CoreTotalScoreExcel coreTotalScoreExcel;

    public CoreTotalScoreForOrgExcelView() {
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String codedFileName = "临时文件";

        Workbook workbook =  new SXSSFWorkbook();


        String title = (String) model.get(TITLE);
        List<JSONObject> contents = (List<JSONObject>) model.get(CONTENT);

        Sheet sheet = workbook.createSheet();

        coreTotalScoreExcel.createSheet(workbook, sheet, title, contents);

        if (model.containsKey(FILE_NAME)) {
            codedFileName = (String)model.get(FILE_NAME);
        }

        codedFileName = codedFileName + ".xls";

        if (this.isIE(request)) {
            codedFileName = URLEncoder.encode(codedFileName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
        }


        response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }



}
